<?php
namespace app\admin\controller;
use app\admin\model\Cate as CateModel;
class Cate extends Common
{
    public function lit()
    {
    	$catemodel = new CateModel;
    	if(request()->isPost()){
    		$post = input('post.');
    		$arr = [];		
    		foreach($post as $key => $val){
    			$arr[] = ['id' => $key , 'sort' => $val];
    		}
    		if($catemodel -> saveAll($arr)){
    			$this -> success('排序成功','lit');
    		}else{
    			$this -> error('排序失败');
    		}
    		return ;
    	}
 
    	$cateRes = $catemodel -> getTreeRes();
    	$this -> assign('cateRes',$cateRes);	
        return $this -> fetch();
    }
    public function add(){
    	if(request()->isPost()){
    		if(db('cate') -> insert(input('post.'))){
    			$this -> success('添加栏目成功','lit');
    		}else{
    			$this -> error('添加栏目失败');
    		}
    	}
    	$catemodel = new CateModel;
    	$cateRes = $catemodel -> getTreeRes();
    	$this -> assign('cateRes',$cateRes);
    	return $this -> fetch();
    }
    public function edit(){
    	if(request()->isPost()){
    		if(db('cate') -> update(input('post.')) !== false){
    			$this -> success('编辑栏目成功','lit');
    		}else{
    			$this -> error('编辑栏目失败');
    		}
    	}
    	$catemodel = new CateModel;
    	$id = input('id');
    	$cateIdRes = db('cate') -> find($id);
    	$cateres = db('cate') 
				    		-> alias('t1')
				    		-> where('t1.id !=' . $id)
				            -> field('t1.* , t2.catename as pidname')
				            -> join('bk_cate t2','t1.pid = t2.id','left') 
				            -> order('sort desc') 
				            -> select();
		$catePid = $catemodel -> getTreeSort($cateres);
    	$this -> assign([
    		'catePid'	=>	$catePid,
    		'cateIdRes'	=>	$cateIdRes,
    	]);
    	return $this -> fetch();
    }
    public function del(){
    	$catemodel = new CateModel;
    	//获取子类id
    	$ChildId = $catemodel -> getChildId(input('id'));
    	$b = $catemodel -> destroy($ChildId);
    	if($b){
    		$this -> success('删除栏目成功','lit');
    	}else{
    		$this -> error('删除栏目失败');
    	}
    }
}
